package com.module.home.ui.healthDataDetails

import android.content.Context
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.common.app.base.AppActivity
import com.common.app.core.RouterHub
import com.common.app.data.bean.KeyBundle
import com.common.app.utls.HeadTitleUtils
import com.common.base.adapter.BaseFragmentPageAdapter
import com.common.base.utils.StatusBarUtils
import com.common.base.widget.indicators.SScaleTransitionPagerTitleView
import com.module.ble.utils.CommonCusHealthType
import com.module.ble.utils.CycleType
import com.module.home.R
import com.module.home.databinding.HomeActivityHealthDataDetailsBinding
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.badge.BadgePagerTitleView

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2024/11/18
 * <p>
 * 描述：健康数据详情页面
 * <p>
 * 修订历史：
 */
@Route(path = RouterHub.ROUTER_HOME_HEALTH_DATA_DETAILS)
class HealthDataDetailsActivity : AppActivity<HomeActivityHealthDataDetailsBinding, HealthDataDetailsViewModel>(),
    View.OnClickListener {

    @Autowired(name = KeyBundle.BUNDLE_KEY)
    @JvmField
    var mType: Int = CommonCusHealthType.HEARTRATE.value

    val titles = ArrayList<String>()

    val dufColor by lazy {when(mType){
            CommonCusHealthType.BLOODOXYGEN.value->getColor(R.color.c_6acd7a)
            CommonCusHealthType.BODYTEMP.value->getColor(R.color.c_61d1ba)
            else -> getColor(R.color.c_f97975) }}

    override fun getViewModel() = HealthDataDetailsViewModel::class.java

    override fun getViewContentBinding() = HomeActivityHealthDataDetailsBinding.inflate(layoutInflater)

    override fun getPageTitle(): String = ""
    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    override fun initViews() {
        super.initViews()

        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)

        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle(when(mType){
            CommonCusHealthType.BLOODOXYGEN.value->getString(com.module.ble.R.string.ble_health_blo_oxy_title)
            CommonCusHealthType.BODYTEMP.value->getString(com.module.ble.R.string.ble_health_body_temper_title)
            else -> getString(com.module.ble.R.string.ble_health_health_rate_title)
        })

        titles.add(getString(R.string.common_day_unit))
        titles.add(getString(R.string.common_week_unit))
        titles.add(getString(R.string.common_month_unit))

        val mFragments = arrayListOf<Fragment>()
//        for (i in titles) mFragments.add(HeartDetailsDayFragment.newInstance(mType, i))
        mFragments.add(HeartDetailsDayFragment.newInstance(mType, CycleType.DAY))
        mFragments.add(HeartDetailsDayFragment.newInstance(mType, CycleType.WEEK))
        mFragments.add(HeartDetailsDayFragment.newInstance(mType, CycleType.MONTH))


        val mAdapter = BaseFragmentPageAdapter(supportFragmentManager, mFragments, titles)
        mViewContentBinding.viewPager.adapter = mAdapter
        mViewContentBinding.viewPager.offscreenPageLimit = mFragments.size
        mAdapter.setNoDestroy(0, 1)

        initMagicIndicator()
    }

    override fun initEvents() {
        super.initEvents()

//        mViewContentBinding.tvTitleRight.setOnClickListener(onClick = this)
    }

    override fun lazyInitData() {
        super.lazyInitData()
        mViewContentBinding.viewPager.currentItem = 0
    }

    override fun onClick(v: View?) {
        when (v) {
//            mViewContentBinding.tvTitleRight -> LaunchHelper.launchWebActivity(ApiConstant.SECKILL_RULE)
        }
    }


    private fun initMagicIndicator() {
        val commonNavigator = CommonNavigator(this)
        commonNavigator.isAdjustMode = true
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return titles.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val badgePagerTitleView = BadgePagerTitleView(context)
                val simplePagerTitleView = SScaleTransitionPagerTitleView(context)
                simplePagerTitleView.text = titles[index]
                simplePagerTitleView.textSize = 16F
                simplePagerTitleView.normalColor = getColor(R.color.text_content_week)
                simplePagerTitleView.selectedColor = dufColor
                simplePagerTitleView.setOnClickListener {
                    mViewContentBinding.viewPager.currentItem = index
                    badgePagerTitleView.badgeView = null // cancel badge when click tab
                }
                badgePagerTitleView.innerPagerTitleView = simplePagerTitleView

                // don't cancel badge when tab selected
                badgePagerTitleView.isAutoCancelBadge = false
                return badgePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_EXACTLY
                indicator.lineHeight = UIUtil.dip2px(context, 5.0).toFloat()
                indicator.lineWidth = UIUtil.dip2px(context, 24.0).toFloat()
                indicator.roundRadius = UIUtil.dip2px(context, 6.0).toFloat()
//                indicator.yOffset = UIUtil.dip2px(context, 2.0).toFloat()
                indicator.setColors(dufColor)
                indicator.startInterpolator = AccelerateInterpolator()
                indicator.endInterpolator = DecelerateInterpolator(2.0f)
                return indicator
            }
        }
        mViewContentBinding.magicIndicator.navigator = commonNavigator
        val titleContainer = commonNavigator.titleContainer // must after setNavigator

        titleContainer.showDividers = LinearLayout.SHOW_DIVIDER_NONE
//        titleContainer.dividerPadding = UIUtil.dip2px(this, 15.0)
//        titleContainer.dividerDrawable = getDrawable(R.drawable.back_down_gray_icon)
        ViewPagerHelper.bind(mViewContentBinding.magicIndicator, mViewContentBinding.viewPager)
    }
}